package com.dollarxy.dlmall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.dollarxy.dlmall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String app_id = "2021000122600131";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCn+y+LC72ULQzoISRVgDMTOD2wxwHhWD+eAAkxCm+vQ3ODU3oyXIAGesyF9ahGDYd02t0whZTHPgXa89KjKq9hQdU1Nw0mrWH45sla8JnHygVSVVdy6Il6A/QPkEf0Odd1uH2GaP10uYaJuK4rA13URKTNs6MKTfVuYWHSVjpx205EiaThIur7Edf3nLx83FrXC7fu/ayybLVAOkvOzdEgvqsAKkUFql/VwyOsI8TX7rsCoqzad/DHPAevNHM/OSraKSbO5H1uRio6n0gBPqin/sqd66NsO5yrwKbJvvaR3Sryh0e66L7eL5/ChbBcfdBezc5wDovZteG/pqsoHRC/AgMBAAECggEBAINWoQidaF+3jZTdfNQhBU4stgdGwhCX+D2oF4exejtViLaQiUVhD3SBhPHkbeo/374c9b5fY0EZHGjiJLoQwMlROMt/vJeWDrkWcS7aWdJdASYrtdERQZQWDTgx/iBVLUJMulBiPHGMMUXzELJu0mkKNrTilEKVS6U/HMXhR5cGsCsnQhaBqUYB/UczFfZk1YGCSvvpDxkwc96IzJSOVtqFEssKodRvQIgKuprXSau3T+VBkL+k8ynXK4rgrLa4Qr+MGyrftLoRxcgeBVwFcC6UYQhVoUsTshuwGpCpK47vyMTY997uWuZxolJlpZ2GsbRVnzbHxf+KtWzYqCALEaECgYEA+nbUzr947Rd7SHHRXwiR1KCXEjGijxJ4CTBOeB4zZ/6JqqSYdcT/K6VAKlfOYGwiLWlTUyb7aL6bVhSbWcghtHql8yCKJcv18XxfXB6JMXAsLp6rjKpE4GJObuPXkSf4eC4dbW6BOM007sjE/hQC65dPcnVBfaB2C7GZF+PD9e8CgYEAq7Gm12liAq/UCPRWzC7lz3ejs8hMBIx1Z1t2XYrtwcNrFiZQqZKMO9ykeVBoQS3mEIvRhrBlOyLmZqnmUx332GphKRrJZTgiye1tiuIPdSZNzXqX4B4uFdLCmQQ3R/TqUf3YBBNUNwplmMy/uN3y5XvK5nd/B3ZkUS2zyEVi4jECgYAfcPln9stUYAU+4+LXnjjIS1yLkSWJ9lSROrovBAOKA37+p+jnp9IshXMXnawvmIN11en630LiNENFvBKAY8ovYNIiQix1cDQwm19RLhgk16MEOzDn2JwK2uxjqUBvXWgolgllqmCYY9JIQ3XPO23ea0Ey6htNjO4kq6e/Qv1fiwKBgCVhx56tDi7hk3vjnyZTq6uRM+5H1O1Em6dGTa4uOnfukVlAPF2/T5MVBiZ3sEtIsAe4OYVm0JOglKOTqFc+diEDgGYPs/u6zjBC5o/PimgKT/RGFd0xFP2hZsckGNks5Pu3FhpFDxfwxpAPPEsG4ATBADtPlzPLvinClpsrKE1xAoGBAL0XYLNOgJUhzkw08ErWn2bdnTkVRBDa6J6ByU6fkiZNChoo3E3HZzPCwPgw1QOd2FjXkD+JiHJ1jc3PYsflot4cC4W1fkofbVIjbA5jkWxKDIt/AD3jljNuDCi1b6M6ZoCX+Tyh2ZzOvxdutPfEJUHmLAhYFLSqlNIETPvE2LuF";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3TdaxMnlgVPjWboMfuktSP8+3kHLPvZFPb2BoEqjqtUHQlSlG0tZ0MsUCnWjMxHz2ovwvujSWwQqy0uqI/bZPWaBJLgPKRzFNKiSkRB4M0uODGe2wm1VP46pfnUOInK9kg4wirAj5dDwFOw+pgErnWGwvX6E0Bp+HRZtkRRvG9KIj2/AQ3548rX/zDK52jwCw008i8ZczqxdYIVU5EsJloG5csGsODdhHElQz+WUCoHPws+X9Z+bhMa/YH2s1SaFYEYG68SpjKJPrva56lnIxLht9RsOudyru0eLN41LhOMG4KIjUxSa+lSVAkia9oEhDIZtA2CBi10SrmWgpRqwIwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url = "http://292q42q864.zicp.fun/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public String return_url = "http://member.dlmall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //订单超时时间
    private String timeout = "5m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl  = "https://openapi.alipaydev.com/gateway.do";;

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ 851 +"\","
                + "\"subject\":\""+ 2211 +"\","
                + "\"body\":\""+ 333 +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");


        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
